/**
 * @file       IEventListener.h
 * @brief      The base class for event listeners
 * @author     Huabo Zheng
 * @date       Jan 13, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#ifndef IEVENTLISTENER_H_
#define IEVENTLISTENER_H_
#include "EventHandler.h"

namespace NS_LCD_AFW
{
class CEventHandler;
class IEvent;
/**
 *NS_LCD_AFW::IEventListener
 */
/**
 *@class  IEventListener
 *@brief  The base class for event listeners
 */
class IEventListener
{
public:

	/**
	 *@brief Constructor
	 *@attention This objects can only be create in the heap memory and call destroy() for destruction
	 *@see destory
	 */
	IEventListener();

	/**
	 *@brief Constructor
	 *@details Constructor
	 *@param[in]  pEventHandler A pointer to a CEventHandler object that responsible for handling events
	 *@param[in]  bAutoRelease Indicate that the object is freed by the caller or by the Callee
	 *@note
	 *@attention This objects can only be create in the heap memory and call destroy() for destruction
	 *@see destory
	*/
	IEventListener(CEventHandler *pEventHandler, bool bAutoRelease = true);

	/**
	 *@brief Constructor
	 *@details Constructor
	 *@param[in]  pEventHandler A pointer to a CEventHandler object that responsible for handling events
	 *@param[in] eventId Specify the event ID that the listener interested in
	 *@param[in] callBack when the interesting event occurs,the listener will call the callBack function to process
	 *@param[in] param Specify the param that pass to the callBack function when event occurs
	 *@param[in]  bAutoRelease Indicate that the object is freed by the caller or by the Callee
	 *@note
	 *@attention This objects can only be create in the heap memory and call destroy() for destruction
	 *@see destory
	*/
	IEventListener(CEventHandler *pEventHandler, int eventId,
			AFW_EventCallBack callBack, int param = INVALID_PARAM,
			bool bAutoRelease = true);
public:

	/**
	 *@brief Judge who is going to release the object
	 *@details Judge who is going to release the object
	 *@exception none
	 *@retval    bool
	 *@return    true if release by callee, otherwise release by caller
	 *@note
	 *@attention
	 *@see setAutoReleaae
	 */
	bool isAutoReleaae() const;

	/**
	 *@brief set who is going to release the object
	 *@details set who is going to release the object
	 *@param[in] bAutoReleaae true if release by callee, otherwise release by caller
	 *@exception none
	 *@retval    void
	 *@return
	 *@note
	 *@attention
	 *@see isAutoReleaae
	 */
	void setAutoReleaae(bool bAutoReleaae);

	/**
	 *@brief  Destory the listener object
	 *@details  Destory the listener object
	 *@exception none
	 *@retval    void
	 *@return    none
	 *@note      none
	 *@attention user must call destroy() for destruction
	 *@see
	*/
	void destory();

	/**
	 *@brief Specified listener object holder
	 *@details Specified listener object holder
	 *@param[in] pOwner A pointer to a CEventHandler object that holds the listener object
	 *@exception none
	 *@retval    void
	 *@return
	 *@note At this stage,a listener object can only be holded by a CEventHandler object
	 *@attention
	 *@see isAutoReleaae
	 */
	void setOwner(CEventHandler *pOwner);

protected:

	/**
	*@brief DeConstructor
	*@attention This objects can only be create in the heap memory and call destroy() for destruction
	*@see destory()
	*/
	virtual ~IEventListener();

public:
	/**
	 *@brief Event dispatcher
	 *@details Dispatch event to the handler or callback function
	 *@param[in] pEvent A pointer to a IEvent object
	 *@exception none
	 *@retval    int
	 *@return    ERR_SUCCESS if success,otherwise failure
	 *@note
	 *@attention
	 *@see
	 */
	virtual int actionPerformed(IEvent *pEvent);
protected:

	/**
	 *@brief Get Current Event Handler
	 *@details Get Current Event Handler
	 *@exception none
	 *@retval    CEventHandler *
	 *@return    A pointer to a CEventHandler object, or NULL if the listener not binded any event handler
	 *@note
	 *@attention
	 *@see
	 */
	CEventHandler *getEventHandler() const;

	/**
	 *@brief Set the Event Handler
	 *@details Set the Event Handler
	 *@param[in] pEventHandler A pointer to a CEventHandler object
	 *@exception none
	 *@retval    CEventHandler *
	 *@return    The old event handler pointer
	 *@note
	 *@attention
	 *@see
	 */
	CEventHandler * setEventHandler(CEventHandler *pEventHandler);

private:
	bool m_bAutoReleaae; ///< set who is going to release the object,true if release by callee, otherwise release by caller
	int m_iEventId;      ///<Event ID of interest
	int m_iParam;        ///<Parameters registered by the user
	AFW_EventCallBack m_pCallBack; ///<Users registered callback function
	CEventHandler * m_pEventHandler;///<Event Handler
	CEventHandler * m_pOwner;//listener holder

private:
	DISABLE_COPY_CONSTRUCTOR_AND_ASSIGN_REFERENCE(IEventListener);
};

} /* namespace NS_LCD_AFW */
#endif /* IEVENTLISTENER_H_ */
